package MathCore.Renders;

import MathCore.Expression;
import MathCore.Lexeme;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Александр
 */
public abstract class Block extends Object
{
	public Block next, prev, parent;

	protected int center=0;
	protected int width, height, x, y;
	protected int flags = 0;
	protected Lexeme link;

	static public final int LEFT=1, 
							RIGHT=2,
							HCENTER=4,
							VCENTER=8, 
							TOP=16,
							BOTTOM=32;

	static public final int TYPE_OPERAND=0,
							TYPE_STOPING_OPERATOR=1,
							TYPE_NON_STOPING_OPERATOR=2,
							TYPE_OPEN_BRACKET=3,
							TYPE_CLOSE_BRACKET=4,
							TYPE_DIVISION=5,
							TYPE_POWER=6;

	static public final int FLAG_CURSOR=2,
							FLAG_ERRROR=4;

	public Block(){}

	abstract public int width();
	abstract public int height();
	abstract public int center();
	abstract public void draw(Graphics g, int x, int y, int style);
	abstract public void extract(Expression exp);
	abstract public void parse();

			 public int x(){return x;}
			 public int y(){return y;}
			 
			 public Block first(){ return null; }
			 public Block last(){  return null; }
			 
			 public void setFlag(int flag) { flags = flags | flag; }
			 public void unsetFlag(int flag) { flags = flags & ~flag; }
			 public int type() { return TYPE_OPERAND; }
	
	public Block findLinked(Lexeme lex)
	{
		if(link == lex) return this;
		return null;
	}
	protected boolean style(int source, int flag)
	{
		return (source & flag) == flag;
	}

	protected boolean flag(int flag)
	{
		return (flags & flag) == flag;
	}

	protected void saveCoordinates(int x, int y, int style)
	{
		if(style(style, HCENTER))	x -= width()/2;
		if(style(style, RIGHT))		x -= width();
		if(style(style, VCENTER))	y -= height() - center();
		if(style(style, BOTTOM))	y -= height();

		this.x = x;
		this.y = y;
	}


	public void dbg_trace()
	{
		if(next == null) System.out.println("\tnext: null");
		else System.out.println("\tfirst: "+next);

		if(prev == null) System.out.println("\tprev: null");
		else System.out.println("\tlast: "+prev);

		if(parent == null) System.out.println("\tparent: null");
		else System.out.println("\tparent: "+parent);
	}
}
